package cn.wolfcode.car.interceptor;

import cn.wolfcode.car.domain.Employee;
import cn.wolfcode.car.util.UserContextUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        if(handler instanceof HandlerMethod){
            Employee currentEmployee = UserContextUtil.getCurrentEmployee();
            if(currentEmployee == null){
                response.sendRedirect("/login.html");
                return false;
            }
        }
        return true;
    }
}
